package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import dao.NewsDao;
import pojo.News;

public class DoUpdateNews extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public DoUpdateNews() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		Map<String, Object> map = new HashMap<String, Object>();
		FileItemFactory df = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(df);
		upload.setHeaderEncoding("utf-8");
		List<FileItem> list = null;
		try {
			list = upload.parseRequest(request);
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (FileItem fi : list) {
			if (fi.isFormField()) {
				map.put(fi.getFieldName(), fi.getString("utf-8"));
			} else if (!fi.isFormField()) {
				if (fi.getName() != null && !fi.getName().equals("")) {
					String picName = fi.getName();
					String houzhui = picName.substring(picName.lastIndexOf("."));
					String newPicName = UUID.randomUUID() + houzhui;
					String filepath = request.getSession().getServletContext().getRealPath("IMG");
					map.put("npicPath", "IMG/" + newPicName);
					try {
						fi.write(new java.io.File(filepath, newPicName));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					map.put("npicPath", "");
				}
			}
		}
		if (NewsDao.updateNews(
				new News((String) (map.get("ntitle")), (String) (map.get("nauthor")), (String) (map.get("npicPath")),
						(String) (map.get("ncontent")), (String) (map.get("nsummary")), new Date(), new Date(),
						Integer.parseInt((String) (map.get("ntid"))), Integer.parseInt((String) (map.get("nid")))))) {
			response.sendRedirect("doShowAdmin?opr=ok");
		} else {
			response.sendRedirect("doModifyNews?nid="+map.get("nid"));
		}
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
